/*************************************************************************
** binned scatter plots of bankruptcy and income (Figure A21)
*************************************************************************/

* sample by year
local yyyyList = "2009 2010 2011 2012 2013 2014 2015 2016"
foreach yyyy in `yyyyList'{
	use "${dataRoot}/bankr_subject_collapsed_`yyyy'06.dta", clear

	set seed 99
	sample 10

	save "${dataRoot}/sample_bankr_subject_collapsed_`yyyy'06.dta", replace
}

* append all years samples
clear
local yyyyList = "2009 2010 2011 2012 2013 2014 2015 2016"
foreach yyyy in `yyyyList'{
	append using "${dataRoot}/sample_bankr_subject_collapsed_`yyyy'06.dta"
}

save "${dataRoot}/pool_allyear_bankr_subject_collapsed.dta", replace


* pooled all year analysis
use "${dataRoot}/pool_allyear_bankr_subject_collapsed.dta",clear
collapse (mean) char* bankr_dum (count) N=bankr_dum, by(zip) fast
rename zip zip5

* merge average income
merge 1:1 zip5 using "${proj}/Data/Raw/ZIP_vars_ACS", keepusing(income) keep(3) nogen

* binned scatter plots
label variable income "Avg. Income"
label variable charp7_dum "Chapter 7 bankruptcy in last 12 months"
label variable charp13_dum "Chapter 13 bankruptcy in last 12 months"
label variable bankr_dum "Bankruptcy in last 12 months"

local xList="income"
local yList="charp7_dum charp13_dum bankr_dum"

foreach var in `yList'{
	replace `var'=`var'*100
}

foreach y in `yList'{
	local yl "`: var label `y''"
	foreach x in `xList'{
		local xl "`: var label `x''"
		binscatter `y' `x', mc(blue) line(none) yti("With `yl' (%)") xti(`xl' ) ///
		ysc(r(0)) ylabel(#8,format(%12.2fc) angle(0) labsize(small)) xlabel(#8,format(%12.0fc) angle(0) labsize(small)) ///
		savegraph("${figGeoDebt}/bins_`x'_`y'_allyear.png") replace
	}
}
